package com.itheima.test2;

public class Demo1Jmm {

    public static void main(String[] args) throws InterruptedException {

        JmmDemo demo = new JmmDemo();
        Thread t = new Thread(demo);
        t.start();
        Thread.sleep(100);
        demo.flag = false;
        System.out.println("已经修改为false");
        System.out.println(demo.flag);
    }

    static class JmmDemo implements Runnable {
//        public boolean flag = true;

        //该成员变量值会先放在主内存中,主线程和子线程都会拿到一个数据副本,
        // volatile关键字会在一个副本发生修改后,通过主内存,将改变后的值传递到其它本地内存
        public volatile boolean flag = true;

        public void run() {
            System.out.println("子线程执行。。。");
            while (flag) {
                //synchronized (this) {
                //}
            }
            System.out.println("子线程结束。。。");
        }
    }
}